//
//  AddNewPaperVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/10.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "AddNewPaperVC.h"
#import "PaperMainHomeVC.h"
#import "SuPhotoPicker.h"

@interface AddNewPaperVC ()<UIScrollViewDelegate,UITextViewDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)NSString *uploadURL;

@end

@implementation AddNewPaperVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"New Article", nil);//新建论文;//@"添加";
    
    [self initNavBarBtnItem];
    
    [self initScrollView];
    
    UITapGestureRecognizer *tapGestureRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView=NO;
    // 将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
}

-(void)keyboardHide:(UITapGestureRecognizer*)tap
{
    UITextField *textField=(UITextField *)[self.view viewWithTag:300];
    [textField resignFirstResponder];

    UITextView *textView=(UITextView *)[self.view viewWithTag:400];
    [textView resignFirstResponder];
}


// UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([@"\n" isEqualToString:text]==YES)
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}


-(void)initScrollView
{
   _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,1,APPW,APPH-64)];
   _scrollView.backgroundColor=WhiteColor;
   _scrollView.delegate=self;
   _scrollView.userInteractionEnabled=YES;
   _scrollView.showsVerticalScrollIndicator=NO;
   [self.view addSubview:_scrollView];
    
    
    //  上传封面图
   UILabel *uploadLabel=[_scrollView getLabelWithText:NSLocalizedString(@"Cover Photo", nil) font:Font_Number(16) textColor:Color333333 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"上传封面图"
    [_scrollView addSubview:uploadLabel];
    [uploadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin*2);
        make.top.mas_equalTo(kmargin);
    }];
    
    UIButton *photoBtn=[UIButton new];
    [_scrollView addSubview:photoBtn];
    photoBtn.tag=200;
    [photoBtn setImage:[UIImage imageNamed:@"photo"] forState:0];
    [photoBtn addTarget:self action:@selector(photoBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    [photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(uploadLabel.mas_bottom).offset(kmargin-2);
        make.left.mas_equalTo(uploadLabel.mas_left);
        make.size.mas_equalTo(CGSizeMake(160/2,160/2));
    }];
    
    // 标题
    UILabel *titleLabel=[_scrollView getLabelWithText:NSLocalizedString(@"ArticleTitle", nil) font:Font_Number(16) textColor:Color333333 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"标题"
    [_scrollView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin*2);
        make.top.mas_equalTo(photoBtn.mas_bottom).offset(kmargin);
    }];
    
    UITextField *textField=[UITextField new];
    [_scrollView addSubview:textField];
    textField.tag=300;
    textField.placeholder=NSLocalizedString(@"EnterTitle", nil);//@"请输入标题";
    textField.textColor=Color999999;
    ViewBorderRadius(textField,kmargin/2,.5,Color979797);
    
    // 设置textField内边距
    [textField setValue:[NSNumber numberWithInt:kmargin] forKey:@"paddingLeft"];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleLabel.mas_left);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(kmargin-2);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*4,44));
    }];
    
    // 正文
    UILabel *textLabel=[_scrollView getLabelWithText:NSLocalizedString(@"MainContent", nil) font:Font_Number(16) textColor:Color333333 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"正文"
    [_scrollView addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(textField.mas_left);
        make.top.mas_equalTo(textField.mas_bottom).offset(kmargin);
    }];
    
    PlaceholderTextView *placeholderTextView=[PlaceholderTextView new];
    [_scrollView addSubview:placeholderTextView];
    placeholderTextView.tag=400;
    placeholderTextView.delegate=self;
    placeholderTextView.placeholder=NSLocalizedString(@"EnterMain", nil);//@"请输入正文内容";
    placeholderTextView.font=Font_Number(16);
    placeholderTextView.textColor=Color999999;
    ViewBorderRadius(placeholderTextView,kmargin/2,.5,Color979797);
    [placeholderTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(textLabel.mas_left);
        make.top.mas_equalTo(textLabel.mas_bottom).offset(kmargin);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*4,kmargin*15));
    }];
    
    // 提交
    UIButton *submitBtn=[UIButton new];
    [self.view addSubview:submitBtn];
    ViewBorderRadius(submitBtn,kmargin/2,2,ClearColor);
    submitBtn.backgroundColor=Main_Color;
    submitBtn.titleLabel.font=Font_Number(16);
    [submitBtn setTitle:NSLocalizedString(@"Submit", nil) forState:UIControlStateNormal];//@"提交"
    [submitBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*2,44));
        make.left.mas_equalTo(kmargin);
        make.top.mas_equalTo(placeholderTextView.mas_bottom).offset(kmargin*3);
    }];
    [submitBtn addTarget:self action:@selector(submitBtnEvent) forControlEvents:UIControlEventTouchUpInside];
}

-(void)submitBtnEvent
{
    UITextField *textField=(UITextField *)[self.view viewWithTag:300];
    UITextView *textView=(UITextView *)[self.view viewWithTag:400];
    
    if (textField.text.length==0)
    {
         [self.view makeToast:@"请输入标题" duration:1.0 position:CSToastPositionCenter];
    }
    else if (textView.text.length==0)
    {
        [self.view makeToast:@"请输入正文" duration:1.0 position:CSToastPositionCenter];
    }
    else
    {
        NSDictionary *dic=@{
                            @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                            @"content":textView.text,
                            @"title":textField.text,
                            @"img":_uploadURL
                            };
        
        [QBRequest AddNewDoctorPublicWithDic:dic Success:^(id res)
         {
             
             [self.view makeToast:res[@"message"] duration:1.0 position:CSToastPositionCenter];
             
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 
                 // 跳转名医发布列表界面
                 PaperMainHomeVC *paperMainHomeVC=[PaperMainHomeVC new];
                 [self.navigationController pushViewController:paperMainHomeVC animated:NO];
                 
             });
             
         } failure:^(NSString *error)
         {
             
         }];
    }
}

-(void)photoBtnEvent
{
    SuPhotoPicker *picker=[[SuPhotoPicker alloc]init];
    picker.selectedCount=1;
    picker.preViewCount=10;
    [picker showInSender:self handle:^(NSArray<UIImage *> *photos)
     {
        [QBRequest UploadImageToQiNiu:photos[0] userId:[Util getUserDefaultObjectForKey:@"id"] success:^(id res)
         {
             [self.view makeToast:@"图片上传成功" duration:1.0 position:CSToastPositionCenter];
              _uploadURL=res;
             UIButton *photoBtn=(UIButton *)[self.view viewWithTag:200];
              [photoBtn setImage:photos[0] forState:0];
             
        } failure:^(NSString *error)
         {
            
        }];
     }];
}

-(void)initNavBarBtnItem
{
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem initWithNormalImage:@"back_gray" target:self action:@selector(backItemEvent)];
}

-(void)backItemEvent
{
    [self.navigationController popViewControllerAnimated:NO];
}

@end
